<template>
	<view class="container" style="display: flex;flex-direction: column;">
		<view class="top" style="width: 730rpx;height: 76rpx;background-color: #FFF;padding-top: 10rpx;padding-left: 20rpx;">
			<u-search placeholder="请输入商品名称" v-model="itemName" @search='search' @custom='search'></u-search>
		</view>
		<!-- 占位 -->
		<view style="width: 750rpx;height: 76rpx;flex-shrink: 0;z-index: -1;"></view>
		<div style="display: flex;flex-direction: row;width: 100%;background-color: #EFEFEF;" class="content">
			<view class="left" style="width: 160rpx;background-color: #EFEFEF;display: flex;flex-direction: column;align-items: center;flex-shrink: 0;">
				<view style="display: flex;flex-direction: column;align-items: center;width: 100%;">
					<view v-for="(item, index) of cateList" :key='index' @click="menuAction(item)" style="height: 50px;width: 100%;line-height: 50px;text-align: center;font-size: 15px;" 
				:style="{
					'color': item.isShow?'#FFF':'#666666',
					'background-color': item.isShow?'#FC5E53':'#EFEFEF',
				}">{{item.name}}</view>
				</view>
			</view>
			<!-- 占位 -->
			<view style="width: 160rpx;flex-shrink: 0;z-index: -1;"></view>
			<view style="width: 590rpx;display: flex;flex-direction: row;flex-wrap: wrap;align-content: flex-start;background-color: #f8f8f8;" class="content">
				<view v-for="(item, index) in itemList" :key='index' @click="itemHandle(item)"
				style="display: flex;flex-direction: column;margin-top: 16rpx;background-color: #FFF;width: 280rpx;margin-left: 10rpx;">
					<image :src="item.coverUrl" mode='aspectFill' style="width: 100%;height: 380rpx;"></image>
					<view style="padding: 16rpx;">
						<view style="font-size: 28rpx;" class="limitLineNumOfTwo">{{item.name}}</view>
						<view style="margin-top: 10rpx;color: #fa3534;">¥{{item.price}}</view>
						<view style="margin-top: 10rpx;color: #999;font-size: 26rpx;">建议零售价：¥{{item.sellingPrice}}</view>
					</view>
				</view>
			</view>
		</div>
	</view>
</template>

<script>
	import {
		page
	} from '@/api/page.js'
	import {
		common
	} from "@/api/common.js";
	import {
		merchant
	} from '@/api/merchant.js'
	export default {
		components: {
		},
		data() {
			return {
				type: '',
				page: 1,
				size: 12,
				showEmpty: false,
				isLastPage: false,
				titleForGroup1: [],
				titleForGroup2: [],
				lastItem: {},
				itemList: [],
				cateList: [],
				nodeModel: {},
				pageConfig: {},
				selectAdvert: [{}],
				itemName: ''
			}
		},
		onLoad() {
			this.getCateListByShop()
		},
		onShow() {

		},
		onPullDownRefresh() {
			this.page = 1
			this.getItemList()
		},
		onReachBottom() {
			console.log('上拉加载更多', this.isLastPage)
			if (this.isLastPage) {
				return
			}
			this.getItemList()
		},
		onShareAppMessage(res) {
			return {
				title: '',
				path: this.$Route.path
			}
		},
		methods: {
			getCateListByShop() {
				merchant.getCateListByShop({
					shopId: this.$Route.query.shop
				}).then(res => {
					if (res.data.code == 10000) {
						this.cateList = res.data.data.list
						if(this.cateList.length>0){
							this.menuAction(this.cateList[0])
						}
					}
				})
			},
			
			menuAction(item){
				console.log('点击菜单', item)
				this.lastItem.isShow = false
				item.isShow = true
				this.lastItem = item
				this.page = 1
				this.isLastPage = false
				this.getItemList()
			},
			search(){
				this.page = 1
				this.getItemList()
			},
			getItemList() {
				merchant.getItemList({
					page: this.page,
					size: this.size,
					cate: this.lastItem.id,
					name: this.itemName,
					status: 1,
					checkStatus: 'PASS'
				}).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if(this.page == 1){
							this.itemList = []
						}
						var list = res.data.data.list
						this.itemList = [...this.itemList, ...list]
						this.showEmpty = this.itemList.length<1
						if (res.data.data.next) {
							this.page += 1
						} else {
							this.isLastPage = true
						}
					}
				})
			},
			itemHandle(item) {
				this.$Router.push({
					name: 'itemDetailForStock',
					params: {
						itemId: item.id
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		height: 100%;
	}
	.top{
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
	}
	.left{
		position: fixed;
		left: 0;
		top: 78rpx;
	}
	.content{
		height: calc(100vh - 78rpx);
	}

</style>
